﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;
using DevComponents.DotNetBar;

namespace QLST
{
    public partial class frmKho : Form
    {
        KhoBUS cls = new KhoBUS();
        KhoDTO p = new KhoDTO();
        Boolean flag;
        public frmKho()
        {
            InitializeComponent();
        }

        private void frmKho_Load(object sender, EventArgs e)
        {
            ClearTextBox();
            LockTextBox(false);
            LockButton(true);
            LoadKho();
        }

        public void LoadKho()
        {
            gvKho.DataSource = cls.LoadKho();
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            frmKho_Load(sender, e);
        }
        private void btnKhoLuu_Click(object sender, EventArgs e)
        {
            if (txtMaNV.Text == "")
            {
                flag = true;
            }
            else if (txtMaNV.Text != "")
            {
                flag = false;
            }
            p.TenKho = txtTen.Text;
            p.SoLuongHienTai = float.Parse(txtHienTai.Text);
            p.SoLuongQuyDinh = float.Parse(txtQuiDinh.Text);

        }
        private void ClearGV()
        {
            gvKho.ClearSelection();
        }

        private void ClearTextBox()
        {
            txtTen.Text = "";
            txtHienTai.Text = "";
            txtQuiDinh.Text = "";
            txtMaNV.Text = "";
        }

        private void LockButton(Boolean f)
        {
            btnNVThem.Enabled = f;
            btnNVSua.Enabled = f;
            btnNVXoa.Enabled = f;
            btnKho.Enabled = !f;
            btnNVHuy.Enabled = !f;
        }

        private void LockTextBox(Boolean f)
        {
            txtTen.Enabled = f;
            txtHienTai.Enabled = f;
            txtQuiDinh.Enabled = f;
            txtMaNV.Enabled = f;
            if (f == false)
            {
                txtTen.BackColor = Color.LemonChiffon;
                txtHienTai.BackColor = Color.LemonChiffon;
                txtQuiDinh.BackColor = Color.LemonChiffon;
                txtMaNV.BackColor = Color.LemonChiffon;
                
            }
            else if (f == true)
            {
                txtTen.BackColor = Color.White;
                txtHienTai.BackColor = Color.White;
                txtQuiDinh.BackColor = Color.White;
                txtMaNV.BackColor = Color.White;
            }
        }

        private void btnNVThem_Click(object sender, EventArgs e)
        {
            LockButton(false);
            LockTextBox(true);
            ClearTextBox();
            gvKho.Enabled = false;
            txtTen.Focus();
        }

        private void btnNVXoa_Click(object sender, EventArgs e)
        {
            p.MaKho  = txtMaNV .Text ;

            cls.Del_Kho(p);
            frmKho_Load (sender, e);
        }

        private void btnNVSua_Click(object sender, EventArgs e)
        {
            LockTextBox(true);
            LockButton(false);
            gvKho.Enabled = false;
            txtTen.Focus();   
        }

        private void gvKho_SelectionChanged(object sender, EventArgs e)
        {
            txtMaNV.Text = gvKho.CurrentRow.Cells["colMa"].Value.ToString();
            txtTen.Text = gvKho.CurrentRow.Cells["colTen"].Value.ToString();
            txtHienTai.Text = gvKho.CurrentRow.Cells["col1"].Value.ToString();
            txtQuiDinh .Text =gvKho .CurrentRow .Cells ["col2"].Value .ToString ();
        }

        private void btnKho_Click(object sender, EventArgs e)
        {
            if (txtMaNV.Text == "")
            {
                flag = true;
            }
            else if (txtMaNV.Text != "")
            {
                flag = false;
            }
            p.MaKho = txtMaNV.Text;
            p.TenKho = txtTen.Text;
            p.SoLuongHienTai = float.Parse ( txtHienTai.Text);
            p.SoLuongQuyDinh = float.Parse(txtQuiDinh.Text);
            cls.IsUp_Kho(p, flag);
            ClearGV();
            frmKho_Load (sender, e);
            gvKho.Enabled = true;
            MessageBoxEx.Show("Cập nhật thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void btnNVHuy_Click(object sender, EventArgs e)
        {
            ClearGV();
            txtTimKiem.Text = "";
            frmKho_Load (sender, e);
            LockButton(true);
            LockTextBox(false);
            gvKho.Enabled = true;
        }

        private void btnNVTim_Click(object sender, EventArgs e)
        {
            p.MaKho  = txtTimKiem.Text;
            p.TenKho  = txtTimKiem.Text;
            if (cbMaKH .Checked  == true)
            {
                flag = true;
            }
            else
            {
                flag = false;
            }
            gvKho.ClearSelection();
            try
            {
                gvKho.DataSource = cls.TimKho(p, flag);
            }
            catch
            {
                MessageBoxEx.Show("Bạn nhập sai Mã KHo hoặc Tên Kho", " CHÚ Ý", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

    }
}
